# -*- python -*-
# ex: set syntax=python:

# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.


from buildbot.scheduler import Scheduler

from master import gitiles_poller
from master import master_config
from master import master_utils
from master import slaves_list
from master.factory import annotator_factory
from master.v8.tools import verify_subdir_slaves

import config
import master_site_config

import math
import os


ActiveMaster = master_site_config.V8
MAIL_NOTIFIER = ActiveMaster.is_production_host

# This is the dictionary that the buildmaster pays attention to. We also use
# a shorter alias to save typing.
c = BuildmasterConfig = {}
c['status'] = []

####### DATABASE

config.DatabaseSetup(c)

####### CHANGESOURCES

comparator = gitiles_poller.GitilesRevisionComparator()
c['change_source'] = [
  gitiles_poller.GitilesPoller(
    'https://chromium.googlesource.com/v8/v8',
    branches=['master'],
    pollInterval=10,
    comparator=comparator,
  ),
]

####### SCHEDULERS

## configure the Schedulers
# v8 Scheduler
s_v8 = Scheduler(name='v8_src',
                 branch='master',
                 treeStableTimer=0,
                 builderNames=['V8 Linux - builder',
                               'V8 Linux - debug builder',
                               'V8 Linux - nosnap builder',
                               'V8 Linux - nosnap debug builder',
                               'V8 Linux - shared',
                               'V8 Linux - vtunejit',
                               'V8 Linux - predictable',
                               'V8 Linux - verify csa',
                               'V8 Linux64 - builder',
                               'V8 Linux64 - custom snapshot - debug builder',
                               'V8 Linux64 - debug builder',
                               'V8 Linux64 - internal snapshot',
                               'V8 Linux64 - cfi',
                               'V8 Linux64 - gcov coverage',
                               'V8 Linux64 - gyp',
                               'V8 Linux64 - verify csa',
                               'V8 Linux gcc 4.8',
                               'V8 Linux64 gcc 4.8 - debug',
                               'V8 Linux64 ASAN',
                               'V8 Linux64 TSAN',
                               'V8 Linux64 TSAN - concurrent marking',
                               'V8 Linux - arm64 - sim - MSAN',
                               'V8 Linux - noi18n - debug',
                               'V8 Win32 - builder',
                               'V8 Win32 - debug builder',
                               'V8 Win32 - nosnap - shared',
                               'V8 Win64',
                               'V8 Win64 - debug',
                               'V8 Win64 - clang',
                               'V8 Mac',
                               'V8 Mac - debug',
                               'V8 Mac64',
                               'V8 Mac64 - debug',
                               'V8 Mac GC Stress',
                               'V8 Mac64 ASAN',
                               'V8 Linux - full debug',
                               'V8 Linux - interpreted regexp',
                               'V8 Random Deopt Fuzzer - debug'])

c['schedulers'] = [s_v8]

builders = []

# ----------------------------------------------------------------------------
# FACTORIES

m_annotator = annotator_factory.AnnotatorFactory(ActiveMaster)

# ----------------------------------------------------------------------------
# BUILDER DEFINITIONS

# The 'builders' list defines the Builders. Each one is configured with a
# dictionary, using the following keys:
#  name (required): the name used to describe this bilder
#  slavename (required): which slave to use, must appear in c['slaves']
#  builddir (required): which subdirectory to run the builder in
#  factory (required): a BuildFactory to define how the build is run
#  periodicBuildTime (optional): if set, force a build every N seconds
#  category (optional): it is not used in the normal 'buildbot' meaning. It is
#                       used by gatekeeper to determine which steps it should
#                       look for to close the tree.
#

CATEGORY_LINUX = '01Linux'
CATEGORY_LINUX64 = '02Linux64'
CATEGORY_WIN = '03Windows'
CATEGORY_MAC = '04Mac'
CATEGORY_GC_STRESS = '05GCStress'
CATEGORY_SANITIZERS = '06Sanitizers'
CATEGORY_MISC = '07Misc'
CATEGORY_FYI = '08FYI'

b_v8_linux_builder = {
  'name': 'V8 Linux - builder',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_LINUX,
  'slavebuilddir': 'linux-builder',
  # Build all revisions on linux 32 release.
  'mergeRequests': False,
}

b_v8_linux_debug_builder = {
  'name': 'V8 Linux - debug builder',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_LINUX,
  'slavebuilddir': 'linux-builder',
}

b_v8_linux_nosnap_builder = {
  'name': 'V8 Linux - nosnap builder',
  'factory':  m_annotator.BaseFactory('v8'),
  'slavebuilddir': 'linux-nosnap-builder',
  'category': CATEGORY_LINUX,
  'mergeRequests': False,
}

b_v8_linux_nosnap_debug_builder = {
  'name': 'V8 Linux - nosnap debug builder',
  'factory':  m_annotator.BaseFactory('v8'),
  'slavebuilddir': 'linux-nosnap-builder',
  'category': CATEGORY_LINUX,
  'slavebuilddir': 'v8-linux-nosnap-debug',
}

b_v8_linux_presubmit = {
  'name': 'V8 Linux - presubmit',
  'slavebuilddir': 'linux',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_LINUX,
}

b_v8_linux = {
  'name': 'V8 Linux',
  'slavebuilddir': 'linux',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_LINUX,
  'auto_reboot' : False,
}

b_v8_linux_debug_avx2 = {
  'name': 'V8 Linux - debug - avx2',
  'slavebuilddir': 'linux',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_LINUX,
  'auto_reboot' : False,
}

b_v8_linux_vtunejit = {
  'name': 'V8 Linux - vtunejit',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_FYI + '|vtunejit',
}

b_v8_linux64_gcov_coverage = {
  'name': 'V8 Linux64 - gcov coverage',
  'slavebuilddir': 'linux64-gcov-coverage',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_FYI,
}

b_v8_linux_predictable = {
  'name': 'V8 Linux - predictable',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_FYI,
}

b_v8_linux_interpreted_regexp = {
  'name': 'V8 Linux - interpreted regexp',
  'builddir': 'v8-linux-interpreted-regexp',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_FYI,
}

b_v8_linux_noi18n_debug = {
  'name': 'V8 Linux - noi18n - debug',
  'builddir': 'v8-linux-noi18n-debug',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_LINUX,
}

b_v8_linux_debug = {
  'name': 'V8 Linux - debug',
  'slavebuilddir': 'linux',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_LINUX,
  'auto_reboot' : False,
}

b_v8_linux_full_debug = {
  'name': 'V8 Linux - full debug',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_FYI,
}

b_v8_linux_shared = {
  'name': 'V8 Linux - shared',
  'slavebuilddir': 'linux-shared',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_LINUX,
}

b_v8_linux_verify_csa = {
  'name': 'V8 Linux - verify csa',
  'slavebuilddir': 'linux-csa',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_LINUX,
}

b_v8_linux64_builder = {
  'name': 'V8 Linux64 - builder',
  'slavebuilddir': 'linux64-builder',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_LINUX64,
  # Build all revisions on linux 64 release.
  'mergeRequests': False,
}

b_v8_linux64_debug_builder = {
  'name': 'V8 Linux64 - debug builder',
  'slavebuilddir': 'linux64-builder',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_LINUX64,
  'mergeRequests': False,
}

b_v8_linux64_custom_snapshot_debug_builder = {
  'name': 'V8 Linux64 - custom snapshot - debug builder',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_LINUX64,
}

b_v8_linux64 = {
  'name': 'V8 Linux64',
  'slavebuilddir': 'linux',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_LINUX64,
  'auto_reboot' : False,
}

b_v8_linux64_debug = {
  'name': 'V8 Linux64 - debug',
  'slavebuilddir': 'linux',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_LINUX64,
  'auto_reboot' : False,
}

b_v8_linux64_avx2 = {
  'name': 'V8 Linux64 - avx2',
  'slavebuilddir': 'linux',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_LINUX64,
  'auto_reboot' : False,
}

b_v8_linux64_debug_avx2 = {
  'name': 'V8 Linux64 - debug - avx2',
  'slavebuilddir': 'linux',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_LINUX64,
  'auto_reboot' : False,
}

b_v8_linux64_internal_snapshot = {
  'name': 'V8 Linux64 - internal snapshot',
  'slavebuilddir': 'linux64-int-snap',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_LINUX64,
}

b_v8_linux64_custom_snapshot_debug = {
  'name': 'V8 Linux64 - custom snapshot - debug',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_LINUX64,
  'auto_reboot' : False,
}

b_v8_linux64_gyp = {
  'name': 'V8 Linux64 - gyp',
  'slavebuilddir': 'linux64-gyp',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_LINUX64,
}

b_v8_linux64_verify_csa = {
  'name': 'V8 Linux64 - verify csa',
  'slavebuilddir': 'linux64-csa',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_LINUX64,
}

b_v8_linux64_custom_snapshot_gcstress = {
  'name': 'V8 Linux64 GC Stress - custom snapshot',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_GC_STRESS,
  'auto_reboot' : False,
}

b_v8_linux_nosnap = {
  'name': 'V8 Linux - nosnap',
  'slavebuilddir': 'linux',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_LINUX,
  'auto_reboot' : False,
}

b_v8_linux_nosnap_debug = {
  'name': 'V8 Linux - nosnap - debug',
  'slavebuilddir': 'linux',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_LINUX,
  'auto_reboot' : False,
}

b_v8_win32_builder = {
  'name': 'V8 Win32 - builder',
  'slavebuilddir': 'win-builder',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_WIN,
}

b_v8_win32 = {
  'name': 'V8 Win32',
  'slavebuilddir': 'swarming',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_WIN,
  'auto_reboot' : False,
}

b_v8_win32_nosnap_shared = {
  'name': 'V8 Win32 - nosnap - shared',
  'builddir': 'v8-win32-nosnap-shared',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_WIN,
}

b_v8_win32_debug_builder = {
  'name': 'V8 Win32 - debug builder',
  'slavebuilddir': 'win-builder',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_WIN,
}

b_v8_win32_debug = {
  'name': 'V8 Win32 - debug',
  'slavebuilddir': 'swarming',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_WIN,
  'auto_reboot' : False,
}

b_v8_mac = {
  'name': 'V8 Mac',
  'slavebuilddir': 'mac',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_MAC,
}

b_v8_mac_debug = {
  'name': 'V8 Mac - debug',
  'slavebuilddir': 'mac',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_MAC,
}

b_v8_mac64 = {
  'name': 'V8 Mac64',
  'slavebuilddir': 'mac64',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_MAC,
}

b_v8_mac64_debug = {
  'name': 'V8 Mac64 - debug',
  'slavebuilddir': 'mac64',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_MAC,
}

b_v8_win64 = {
  'name': 'V8 Win64',
  'slavebuilddir': 'win64',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_WIN,
}

b_v8_win64_debug = {
  'name': 'V8 Win64 - debug',
  'slavebuilddir': 'win64',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_WIN,
}

b_v8_win64_clang = {
  'name': 'V8 Win64 - clang',
  'slavebuilddir': 'win64-clang',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_WIN,
}

b_v8_fuzz = {
  'name': 'V8 Fuzzer',
  'slavebuilddir': 'linux',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_MISC,
  'auto_reboot' : False,
}

b_v8_linux_deopt_fuzzer = {
  'name': 'V8 Deopt Fuzzer',
  'slavebuilddir': 'linux',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_MISC,
  'auto_reboot' : False,
}

b_v8_linux_random_deopt_fuzzer_debug = {
  'name': 'V8 Random Deopt Fuzzer - debug',
  'builddir': 'v8-linux-random-deopt-fuzzer-debug',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_FYI,
}

b_v8_gcstress = {
  'name': 'V8 Linux - gc stress',
  'slavebuilddir': 'linux',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_GC_STRESS,
  'auto_reboot' : False,
}

b_v8_mac_gcstress = {
  'name': 'V8 Mac GC Stress',
  'slavebuilddir': 'mac',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_GC_STRESS,
}

b_v8_linux_gcc_4_8 = {
  'name': 'V8 Linux gcc 4.8',
  'builddir': 'v8-linux-gcc',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_MISC,
}

b_v8_linux64_gcc_4_8_debug = {
  'name': 'V8 Linux64 gcc 4.8 - debug',
  'builddir': 'v8-linux64-gcc',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_MISC,
}

b_v8_linux64_asan = {
  'name': 'V8 Linux64 ASAN',
  'builddir': 'v8-linux64-asan',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_SANITIZERS,
}

b_v8_linux64_tsan = {
  'name': 'V8 Linux64 TSAN',
  'builddir': 'v8-linux64-tsan',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_SANITIZERS,
}

b_v8_linux64_tsan_concurrent_marking = {
  'name': 'V8 Linux64 TSAN - concurrent marking',
  'builddir': 'v8-linux64-tsan-concurrent-marking',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_SANITIZERS,
}

b_v8_linux64_cfi = {
  'name': 'V8 Linux64 - cfi',
  'slavebuilddir': 'linux64-cfi',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_FYI,
}

b_v8_linux_arm64_sim_msan = {
  'name': 'V8 Linux - arm64 - sim - MSAN',
  'builddir': 'v8-linux-arm64-sim-msan',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_SANITIZERS,
}

b_v8_mac64_asan = {
  'name': 'V8 Mac64 ASAN',
  'slavebuilddir': 'mac64-asan',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_SANITIZERS,
}

c['builders'] = [b_v8_linux_builder,
                 b_v8_linux_debug_builder,
                 b_v8_linux_nosnap_builder,
                 b_v8_linux_nosnap_debug_builder,
                 b_v8_linux_presubmit,
                 b_v8_linux,
                 b_v8_linux_debug,
                 b_v8_linux_shared,
                 b_v8_linux_nosnap,
                 b_v8_linux_nosnap_debug,
                 b_v8_linux_noi18n_debug,
                 b_v8_linux_debug_avx2,
                 b_v8_linux_verify_csa,
                 b_v8_linux64_builder,
                 b_v8_linux64_debug_builder,
                 b_v8_linux64_custom_snapshot_debug_builder,
                 b_v8_linux64,
                 b_v8_linux64_avx2,
                 b_v8_linux64_internal_snapshot,
                 b_v8_linux64_gyp,
                 b_v8_linux64_debug,
                 b_v8_linux64_custom_snapshot_debug,
                 b_v8_linux64_debug_avx2,
                 b_v8_linux64_verify_csa,
                 b_v8_win32_builder,
                 b_v8_win32_debug_builder,
                 b_v8_win32,
                 b_v8_win32_nosnap_shared,
                 b_v8_win32_debug,
                 b_v8_win64,
                 b_v8_win64_debug,
                 b_v8_win64_clang,
                 b_v8_mac,
                 b_v8_mac_debug,
                 b_v8_mac64,
                 b_v8_mac64_debug,
                 b_v8_gcstress,
                 b_v8_linux64_custom_snapshot_gcstress,
                 b_v8_mac_gcstress,
                 b_v8_linux64_asan,
                 b_v8_linux64_tsan,
                 b_v8_linux64_tsan_concurrent_marking,
                 b_v8_linux64_cfi,
                 b_v8_linux_arm64_sim_msan,
                 b_v8_mac64_asan,
                 b_v8_fuzz,
                 b_v8_linux_deopt_fuzzer,
                 b_v8_linux_gcc_4_8,
                 b_v8_linux64_gcc_4_8_debug,
                 b_v8_linux_vtunejit,
                 b_v8_linux64_gcov_coverage,
                 b_v8_linux_predictable,
                 b_v8_linux_full_debug,
                 b_v8_linux_interpreted_regexp,
                 b_v8_linux_random_deopt_fuzzer_debug]

# Associate the slaves to the builders. The configuration is in slaves.cfg.
slaves = slaves_list.SlavesList('slaves.cfg', 'V8')
for builder in c['builders']:
  builder['slavenames'] = slaves.GetSlavesName(builder=builder['name'])


####### BUILDSLAVES

# The 'slaves' list defines the set of allowable buildslaves. List all the
# slaves registered to a builder. Remove dupes.
c['slaves'] = master_utils.AutoSetupSlaves(c['builders'],
                                           config.Master.GetBotPassword())

# Make sure everything works together.
# TODO(phajdan.jr): Make slave pools sane, http://crbug.com/435559 .
master_utils.VerifySetup(c, slaves, enforce_sane_slave_pools=False)

# Make sure subdir slaves don't auto-reboot.
verify_subdir_slaves(c)

####### STATUS TARGETS

# TODO(machenbach): I think the next line is not used and has no effect:
# Adds common status and tools to this master.
# Buildbot master url:
# Must come before AutoSetupMaster().
c['buildbotURL'] = ActiveMaster.buildbot_url
master_utils.AutoSetupMaster(c, ActiveMaster,
    public_html='../master.chromium/public_html',
    tagComparator=comparator,
    templates=['./templates', '../master.chromium/templates'])

if MAIL_NOTIFIER:
  import mail_notifier_cfg
  mail_notifier_cfg.Update(config, ActiveMaster, c)

# Do it at the end to override values set by AutoSetupMaster, the default is
# too low. Must keep at least a few days worth of builds.
c['buildHorizon'] = 1000
c['logHorizon'] = 200
# Must be at least 2x the number of slaves.
c['eventHorizon'] = 200

####### PROJECT IDENTITY

c['projectName'] = ActiveMaster.project_name
c['projectURL'] = config.Master.project_url
